package com.haina.shop.controller.user;

import com.haina.shop.context.BaseContext;
import com.haina.shop.domain.Area;
import com.haina.shop.domain.UserAddr;
import com.haina.shop.domain.UserAddrOrder;
import com.haina.shop.result.ServiceResult;
import com.haina.shop.service.AreaService;
import com.haina.shop.service.UserAddrOrderService;
import com.haina.shop.service.UserAddrService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/p/address")
public class AddressController {

    @Resource
    private UserAddrService userAddrService;

    @Resource
    private UserAddrOrderService userAddrOrderService;


    @RequestMapping("/list")
    public ServiceResult address(){
        List<UserAddr> addrList = userAddrService.list();
        return ServiceResult.success(addrList);
    }

    @PostMapping("/addAddr")
    public ServiceResult addAddr(@RequestBody UserAddr addrParam){
        Long userId = BaseContext.getCurrentId();
        addrParam.setUserId(String.valueOf(userId));
        addrParam.setStatus(1);
        addrParam.setCreateTime(new Date());
        addrParam.setUpdateTime(new Date());
        addrParam.setVersion(0);
        int insert = userAddrService.insert(addrParam);
        if(insert>0){
            System.out.println("ok");
        }
        return ServiceResult.success();
    }

    @PutMapping("/defaultAddr/{addrId}")
    public ServiceResult setCommonAddr(@PathVariable("addrId") int addrId){
        int i = userAddrService.updComAddr(addrId);
        return ServiceResult.success();
    }

    @GetMapping("/addrInfo/{addrId}")
    public ServiceResult selUserAddr(@PathVariable("addrId") int addrId){
        UserAddr userAddr = userAddrService.selAddrId(addrId);
        return ServiceResult.success(userAddr);
    }

    @DeleteMapping("/deleteAddr/{addrId}")
    public ServiceResult delUserAddr(@PathVariable("addrId") int addrId){
        int i = userAddrService.deleteAddr(addrId);
        return ServiceResult.success();
    }

    @PutMapping("/updateAddr")
    public ServiceResult updUserAddr(@RequestBody UserAddr addrParam){
        addrParam.setUpdateTime(new Date());
        addrParam.setVersion(0);
        int i = userAddrService.updAddr(addrParam);
        return ServiceResult.success();
    }
}
